using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;
using static UnityEditor.Progress;

public class CharacterUI : UIBase
{
    public Text[] characterDataTexts;
    public Image[] equipSlots;
    public Sprite equipSlotDeafultBg;
    private ScrollRect scrollRect;
    private Button equipBtn;
    private GameObject unEquipBtn;
    private List<GameObject> equipitems = new List<GameObject>();
    private GameObject itemPrefab;
    private StatType[] dataTypes = new StatType[]
    {
        StatType.strength, StatType.agility, StatType.intelegence,StatType.vitality,
        StatType.damage,StatType.health,StatType.armor,StatType.critChance,StatType.critPower,
        StatType.evasion,StatType.magicRes,StatType.fireDamage,StatType.iceDamage,StatType.lightingDamage
    };
    private Player player;
    private ItemData selectItem;


    private void Awake()
    {
        itemPrefab = Resources.Load<GameObject>("Prefabs/UI/BackpackItemUI");
        scrollRect = transform.Find("CharacterEquips/Scroll View").GetComponent<ScrollRect>();
        equipBtn = transform.Find("CharacterEquips/EquipButton").GetComponent<Button>();
        unEquipBtn = equipBtn.transform.Find("UnEquipButton").gameObject;
    }

    protected override void Start()
    {
        base.Start();
        UpdateDataPanelInfo();
        UpdateCharacterEquipInfo();
        UpdateEquipInfo();
        equipBtn.onClick.AddListener(() =>
        {
            BackpackMgr.Instance.EquipItem(selectItem as EquipData);
            SetEquipBtnStatus(true);
        });
        unEquipBtn.GetComponent<Button>().onClick.AddListener(() =>
        {
            BackpackMgr.Instance.UnEquipItem(selectItem as EquipData);
            SetEquipBtnStatus(false);
        });

        BackpackMgr.Instance.onEquipChanged += UpdateCharacterEquipInfo;
        BackpackMgr.Instance.onEquipChanged += UpdateDataPanelInfo;
    }

    void UpdateDataPanelInfo()
    {
        player = PlayerManager.Instance.Player;
        for (int i = 0; i < characterDataTexts.Length; i++)
        {
            if (i == characterDataTexts.Length - 1)
            {
                characterDataTexts[i].text = player.data.currentHealth.ToString();
                return;
            }

            if (player.data.GetData(dataTypes[i]).GetModify() != 0)
            {
                characterDataTexts[i].text = player.data.GetData(dataTypes[i]).GetValue().ToString() + "<color=green>" + "+" + player.data.GetData(dataTypes[i]).GetModify().ToString() + "</color>"; ;
            }
            else
            {
                characterDataTexts[i].text = player.data.GetData(dataTypes[i]).GetValue().ToString();
            }

        }
    }


    void UpdateCharacterEquipInfo()
    {
        Dictionary<int,EquipData> equips = new Dictionary<int,EquipData>();
        foreach (var equip in BackpackMgr.Instance.equipItems)
        {
            switch (equip.equipType) 
            {
                case E_EquipType.Weapon:
                    equips.Add(0,equip);                   
                    break;
                case E_EquipType.Armor:
                    equips.Add(1,equip);
                    break;
                case E_EquipType.Jewelry:
                    equips.Add(2,equip);
                    break;
                case E_EquipType.Book:
                    equips.Add(3,equip);
                    break;
            }
        }
        for (int i = 0; i < equipSlots.Length; i++) 
        {
            if (equips.ContainsKey(i))
            {
                equipSlots[i].sprite = equips[i].icon;
            }
            else
            {
                equipSlots[i].sprite = equipSlotDeafultBg;
            }
        }
    }

    void UpdateEquipInfo()
    {   
        foreach(var item in equipitems)
        {
            Destroy(item);
        }
        equipitems.Clear();
        foreach (var item in BackpackMgr.Instance.backpackItems)
        {
            if (item.Value.itemData.type == E_ItemType.equipment)
            {
                GameObject go = Instantiate(itemPrefab, scrollRect.content);
                equipitems.Add(go);
                go.GetComponent<BackpackItemUI>().SetItemInfo(item.Value.itemData, item.Value.count,this);
            }
        }

        scrollRect.content.sizeDelta = new Vector2(-(805 - 90 - 110 * equipitems.Count), 140);
    }

    public override void UpdateItemDetail(ItemData _itemData)
    {
        selectItem = _itemData;
        bool select;
        foreach(var item in equipitems)
        {
            select = item.GetComponent<BackpackItemUI>().itemData == _itemData;
            item.GetComponent<BackpackItemUI>().IsSelected(item.GetComponent<BackpackItemUI>().itemData == _itemData);

        }
        SetEquipBtnStatus(BackpackMgr.Instance.equipItems.Contains(_itemData as EquipData));
    }

    void SetEquipBtnStatus(bool equip)
    {
        unEquipBtn.SetActive(equip);
    }

    public override void ShowMe()
    {
        base.ShowMe();
        UpdateDataPanelInfo();
        UpdateCharacterEquipInfo();
        UpdateEquipInfo();
    }


}
